﻿namespace OxWebCATI.Monitoring
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;

    [ComVisible(true), Guid("F5E22AC6-8D8A-4424-A68F-3CCB12EC82DE")]
    public class UserMonitorData
    {
        internal SortedList<long, MonitorData> _EventData = new SortedList<long, MonitorData>();
        internal long _IndexProvider;
        internal long _LastDeletedEventIndex;
        internal long _LatestHtmlIndex;
        internal long _TemporaryHtmlIndex;

        [ComVisible(true)]
        public void AppendEvents(string eventData)
        {
            this._IndexProvider += 1L;
            MonitorData data = new MonitorData(false) {
                _Data = eventData
            };
            this._EventData.Add(this._IndexProvider, data);
        }

        [ComVisible(true)]
        public void AppendHtml(string html)
        {
            if (this._TemporaryHtmlIndex != 0L)
            {
                MonitorData local1 = this._EventData[this._TemporaryHtmlIndex];
                local1._Data = local1._Data + html;
            }
        }

        [ComVisible(true)]
        public void CompletePage()
        {
            this._LatestHtmlIndex = this._TemporaryHtmlIndex;
        }

        [ComVisible(true)]
        public string GetData(string dataIndex)
        {
            return this._EventData[Convert.ToInt64(dataIndex)]._Data;
        }

        [ComVisible(true)]
        public bool IsDataAvailable(string dataIndex)
        {
            return this._EventData.ContainsKey(Convert.ToInt64(dataIndex));
        }

        [ComVisible(true)]
        public bool IsHtml(string dataIndex)
        {
            return this._EventData[Convert.ToInt64(dataIndex)]._IsHtml;
        }

        [ComVisible(true)]
        public void PreparePage()
        {
            long lastOutdatedEventIndex = this.LastOutdatedEventIndex;
            if (lastOutdatedEventIndex > 0L)
            {
                for (long i = this._LastDeletedEventIndex; i <= lastOutdatedEventIndex; i += 1L)
                {
                    this._EventData.Remove(i);
                }
                this._LastDeletedEventIndex = lastOutdatedEventIndex;
            }
            this._IndexProvider += 1L;
            this._EventData.Add(this._IndexProvider, new MonitorData(true));
            this._TemporaryHtmlIndex = this._IndexProvider;
        }

        private long LastOutdatedEventIndex
        {
            get
            {
                for (long i = this._IndexProvider; i > this._LastDeletedEventIndex; i -= 1L)
                {
                    if (this._EventData[i]._RegisterDate.AddSeconds(3.0) < DateTime.Now)
                    {
                        return i;
                    }
                }
                return 0L;
            }
        }

        [ComVisible(true)]
        public string LatestHtmlIndex
        {
            get
            {
                return this._LatestHtmlIndex.ToString();
            }
        }
    }
}

